package org.zjvis.graph.analysis.service.exception;

import lombok.Getter;
import org.zjvis.graph.analysis.service.model.util.ApiResult;
import org.zjvis.graph.analysis.service.enums.ApiResultCode;

@Getter
public class GraphAnalysisException extends RuntimeException {

    private static final long serialVersionUID = 4465566270019332160L;

    private ApiResult apiResult;

    public GraphAnalysisException(String message) {
        super(message);
        this.apiResult = ApiResult.valueOf(ApiResultCode.SYS_ERROR, message);
    }

    public GraphAnalysisException(String message, Throwable cause) {
        super(message, cause);
        this.apiResult = ApiResult.valueOf(ApiResultCode.SYS_ERROR, message);
    }

    public GraphAnalysisException(Throwable cause) {
        super(cause);
        this.apiResult = ApiResult.valueOf(ApiResultCode.SYS_ERROR);
    }

    public GraphAnalysisException(Integer code, String msg, String info, Throwable cause) {
        super(msg, cause);
        if (info == null) {
            this.apiResult = new ApiResult(code, msg);
        } else {
            this.apiResult = new ApiResult(code, msg + ":" + info);
        }
    }

    public GraphAnalysisException(ErrorCode errorCode, Throwable cause) {
        this(errorCode.getCode(), errorCode.getMsg(), null, cause);
    }

    public GraphAnalysisException(ErrorCode errorCode, String info, Throwable cause) {
        this(errorCode.getCode(), errorCode.getMsg(), info, cause);
    }

    public static GraphAnalysisException of(ErrorCode errorCode, String info) {
        return new GraphAnalysisException(errorCode, info, null);
    }

    public static GraphAnalysisException of(ErrorCode errorCode, String info, String tips) {
        GraphAnalysisException e = new GraphAnalysisException(errorCode, info, null);
        e.getApiResult().setTips(tips);
        return e;
    }

    public GraphAnalysisException(ErrorCode errorCode) {
        this(errorCode, null);
    }

    public GraphAnalysisException(Integer code, String msg) {
        this.apiResult = new ApiResult(code, msg);
    }

    public GraphAnalysisException(ApiResultCode code, String message) {
        super(message);
        this.apiResult = ApiResult.valueOf(code, message);
    }

}
